package com.sky.loveshop.controller;

import com.sky.loveshop.entity.Category;
import com.sky.loveshop.entity.Result;
import com.sky.loveshop.entity.common.PageResult;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.exception.BizExceptionEnum;
import com.sky.loveshop.entity.qo.CategoryQO;
import com.sky.loveshop.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/8/18 0:04
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/listCategoryPages")
    public Result<PageResult<Category>> listCategoryPages(@RequestBody CategoryQO categoryQO) {
        return Result.page(categoryService.listCategoryPages(categoryQO));
    }

    @PostMapping("/listCategory")
    public Result<List<Category>> listCategory(@RequestBody CategoryQO categoryQO) {
        return Result.success(categoryService.listCategory(categoryQO));
    }

    @PutMapping("/addCategory")
    public Result<Integer> addCategory(@RequestBody @Valid Category category) {
        return Result.success(categoryService.addCategory(category));
    }

    @DeleteMapping("/delCategoryById")
    public Result<Integer> delCategoryById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(categoryService.delCategoryById(id));
    }

    @PostMapping("/editCategory")
    public Result<Integer> editCategory(@RequestBody Category category) throws BizException {
        if (category.getId() == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(categoryService.updateCategoryById(category));
    }

    @GetMapping("/getCategoryById")
    public Result<Category> getCategoryById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(categoryService.getCategoryById(id));
    }

}
